<?php
namespace Admin\Controller;
use Org\Yike\Category;
use Org\Yike\Tree;
use Org\Yike\Pinyin;
class LinkmenuController extends CommonController {
    public function _initialize() {
        parent::_initialize();
        //import('ORG.Yike.Tree');
        $this->_mod = D('Linkmenu');
    }

    public function index() {
        $sort = I("request.sort", 'ordid');
        $order = I("request.order", 'ASC');
        $menuid = I('get.id',0,'int');
        $where  = array();
        if($menuid){$where = array('menuid'=>$menuid);}
        $tree = new Tree();
        $tree->icon = array('│ ','├─ ','└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        if($where){
            $this->_mod->where($where);
        }
        $result = $this->_mod->order($sort . ' ' . $order)->select();
        $array = array();
        foreach($result as $r) {
            $r['str_status'] = '<img data-tdtype="toggle" data-id="'.$r['id'].'" data-field="status" data-value="'.$r['status'].'" src="./Public/Admin/images/admin/toggle_' . ($r['status'] == 0 ? 'disabled' : 'enabled') . '.gif" />';
            $r['str_manage'] = '<a href="javascript:;" class="J_showdialog" data-uri="'.U('Linkmenu/add',array('pid'=>$r['id'],'id'=>$menuid)).'" data-title="'.L('add_article_cate').'" data-id="add" data-width="500" data-height="360">'.L('add_article_subcate').'</a> |
                                <a href="javascript:;" class="J_showdialog" data-uri="'.U('Linkmenu/edit',array('id'=>$r['id'],'menuid'=>$menuid)).'" data-title="'.L('edit').' - '. $r['name'] .'" data-id="edit" data-width="500" data-height="360">'.L('edit').'</a> |
                                <a href="javascript:;" data-acttype="ajax" class="J_confirmurl" data-uri="'.U('Linkmenu/delete',array('id'=>$r['id'])).'" data-msg="'.sprintf(L('confirm_delete_one'),$r['name']).'">'.L('delete').'</a>';
            $r['parentid_node'] = ($r['pid'])? ' class="child-of-node-'.$r['pid'].'"' : '';
            $r['cate_type'] = $r['type'] ? '<span class="blue">'.L('article_cate_type_'.$r['type']).'</span>' : L('article_cate_type_'.$r['type']);
            $img = $r['img'] ? '<img src="'.$r['img'].'" width="50" />' : '';
            $dispos = $r['dispos'] == 1 ? '推荐' : '';
            $hot    = $r['ishot'] == 1 ? '热门' : '';
            $r['str_pos'] = $dispos.'/'.$hot.$img;
            $array[] = $r;
        }
        $str  = "<tr id='node-\$id' \$parentid_node>
                <td align='center'><input type='checkbox' value='\$id' class='J_checkitem'></td>
                <td align='center'>\$id</td>
                <td>\$spacer<span data-tdtype='edit' data-field='name' data-id='\$id' class='tdedit'>\$name</span></td>
				<td align='center'>\$alias</td>
                <td align='center'><span data-tdtype='edit' data-field='ordid' data-id='\$id' class='tdedit'>\$ordid</span></td>
                <td align='center'>\$str_pos</td>
                <td align='center'>\$str_status</td>
                <td align='center'>\$str_manage</td>
                </tr>";
        $tree->init($array);
        $list = $tree->get_tree(0, $str);
        $this->assign('list', $list);
        //bigmenu (标题，地址，弹窗ID，宽，高)
        $big_menu = array(
            'title' => '添加联动菜单',
            'iframe' => U('Linkmenu/add',array('id'=>$menuid)),
            'id' => 'add',
            'width' => '500',
            'height' => '360'
        );
        $this->assign('big_menu', $big_menu);
        $this->assign('list_table', true);
        $this->display();
    }

    /**
     * 添加子菜单上级默认选中本栏目
     */
    public function _before_add() {
        $menuid = I('id',0,'int');
        $pid = I('pid',0,'intval');
        if ($pid) {
            $spid = $this->_mod->where(array('id'=>$pid))->getField('spid');
            $spid = $spid ? $spid.$pid : $pid;
            $this->assign('spid', $spid);
        }
        $this->assign('menuid',$menuid);
    }
    public function _before_edit() {
        $menuid = I('menuid',0,'int');
        $pid = I('pid',0,'intval');
        if ($pid) {
            $spid = $this->_mod->where(array('id'=>$pid))->getField('spid');
            $spid = $spid ? $spid.$pid : $pid;
            $this->assign('spid', $spid);
        }
        $this->assign('menuid',$menuid);
    }
    /**
     * 入库数据整理
     */
    protected function _before_insert($data = '') {
        $namearr = explode("\n",str_replace('&',"\n",trim($data['name'])));
        $py=new Pinyin();
        $adddata = array();
        foreach($namearr as $v){
            //检测分类是否存在
            if($this->_mod->name_exists($data['name'], $data['pid'],$data['menuid'])){
               unset($v);
            }else{
                //生成spid
                $data['name'] = $v;
                $data['spid'] = $this->_mod->get_spid($data['pid']);
                $data['alias']=$py->getAllPY($data['name']);		//全部拼音
                $adddata[] = $data;
            }
        }
       $this->addAll = true;
        return $adddata;

    }

    /**
     * 修改提交对数据
     */
    protected function _before_update($data = '') {
        if ($this->_mod->name_exists($data['name'], $data['pid'], $data['menuid'],$data['id'])) {
            $this->ajaxReturn(0, L('article_cate_already_exists'));
        }
        $old_pid = $this->_mod->field('pid')->where(array('id'=>$data['id']))->find();
        if ($data['pid'] != $old_pid['pid']) {
            //不能把自己放到自己或者自己的子目录们下面
            $wp_spid_arr = $this->_mod->get_child_ids($data['id'], true);
            if (in_array($data['pid'], $wp_spid_arr)) {
                $this->ajaxReturn(0, L('cannot_move_to_child'));
            }
            //重新生成spid
            $data['spid'] = $this->_mod->get_spid($data['pid']);
        }
        //栏目别名
        if($data['alias']==''){
            //import("ORG.Yike.Pinyin");
            $py=new Pinyin();
            if($_POST['aliasRadio']==1){
                $data['alias']=$py->getFirstPY($data['name']);	//首字母拼音
            }else{
                $data['alias']=$py->getAllPY($data['name']);		//全部拼音
            }
        }
        return $data;
    }

    /**
     * 获取紧接着的下一级分类ID
     */


    private function setCateInCache($menuid){
       // $cate_list = $this->_mod->field('id,pid,name')->where('display=1 and menuid='.$menuid)->select();
       // $cateTree  = Category::getCateTreeArr($cate_list,0);
       // F('menucate_'.$menuid,$cateTree);
    }
}